const express = require('express')
const router =express.Router()
const NewsModel = require('../models/news')

router.get('/find',(req,res,next)=>{
    
      NewsModel.find().then(result=>{
        res.json({
            code:200,
            data:result,
            message:'全局查询成功',
        })
    })
})

router.post('/create',(req,res,next)=>{
    const {content,title,pic} = req.body
    NewsModel.find().then(result=>{
        const order = new NewsModel({
          news_id:result.length?result[result.length-1].news_id+1:0,
          content,
          title,
          pic,
          create_time:new Date(new Date().valueOf()+8*60*60*1000),
        })
        order.save().then(result => {
            res.json({
                code:200,
                data:{
                    result
                },
                message:'order创建完成',
            })
        })
    })
})

router.post('/update',(req,res,next)=>{
    const {news_id,content,title} = req.body
  
    NewsModel.updateOne({news_id},{$set:{"content":content,"title":title}}).then(result=>{
          res.json({
              code:200,
              data:{
                  result
              },
              message:'修改完成',
          })
      })
  })

router.delete('/delete',(req,res,next)=>{
  const {news_id} = req.body

  NewsModel.deleteOne({news_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'删除完成',
      })
  })
})
module.exports = router